package nio.SocketChannel;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.concurrent.TimeUnit;

public class Client {
	public static void main(String[] args) throws IOException, InterruptedException {
		ByteBuffer buffer = ByteBuffer.allocate(35);

		SocketChannel socketChannel = SocketChannel.open();
		socketChannel.configureBlocking(false);
		socketChannel.connect(new InetSocketAddress("127.0.0.1", 8080));
		if (socketChannel.finishConnect()) {
			int i = 0;
			while (true) {
				TimeUnit.SECONDS.sleep(1);
				String info = "I'm " + i++ + "-th information from client";
				buffer.clear();
				buffer.put(info.getBytes());
				buffer.flip();
				while (buffer.hasRemaining()) {
					System.out.println(buffer);
					socketChannel.write(buffer);
				}
			}
		}

		socketChannel.close();
	}
}
